#!/bin/bash
clear
if [[ "$EUID" -ne 0 ]]; then
    echo -e "\033[1;31mScript need to be run as root!\033[0m"; exit 1
fi

apt-get -qq update
wget -q -O - https://raw.githubusercontent.com/XTLS/Xray-install/main/install-release.sh | bash

touch /usr/local/etc/xray/.xray

cp /etc/letsencrypt/live/cybertize.ml/fullchain.pem /usr/local/etc/xray/xray.crt
cp /etc/letsencrypt/live/cybertize.ml/privkey.pem /usr/local/etc/xray/xray.key
chmod 644 /usr/local/etc/xray/xray.crt; chmod 644 /usr/local/etc/xray/xray.key

alamat_ip=$(wget -qO- ipv4.icanhazip.com)
alamat_port=$(shuf -i 1025-9999 -n 1)
alamat_hos=$(cat /etc/environment | grep 'DOMAIN' | cut -d '=' -f 2 | head -n 1)
alamat_emel=$(cat /etc/environment | grep 'EMAIL' | cut -d '=' -f 2)
user_uid="$(cat '/proc/sys/kernel/random/uuid')"
user_aid=$(shuf -i 1-254 -n 1)

cat > xray-xtls.json <<-xray-xtls
{
    "log": {
        "loglevel": "warning"
    },
    "inbounds": [
        {
            "port": $alamat_port,
            "protocol": "vless",
            "settings": {
                "clients": [
                    {
                        "id": "1d716ea0-9670-4359-800b-ed8d783ec04b",
                        "flow": "xtls-rprx-direct",
                        "level": 0,
                        "email": "$alamat_emel"
                    }
                ],
                "decryption": "none",
                "fallbacks": [
                    {
                        "dest": 1310,
                        "xver": 1
                    },
                    {
                        "path": "/websocket",
                        "dest": 1234,
                        "xver": 1
                    },
                    {
                        "path": "/vmesstcp",
                        "dest": 2345,
                        "xver": 1
                    },
                    {
                        "path": "/vmessws",
                        "dest": 3456,
                        "xver": 1
                    }
                ]
            },
            "streamSettings": {
                "network": "tcp",
                "security": "xtls",
                "xtlsSettings": {
                    "alpn": [
                        "http/1.1"
                    ],
                    "certificates": [
                        {
                            "certificateFile": "/usr/local/etc/v2ray/v2ray.crt",
                            "keyFile": "/usr/local/etc/v2ray/v2ray.key"
                        }
                    ]
                }
            }
        },
        {
            "port": 1310,
            "listen": "127.0.0.1",
            "protocol": "trojan",
            "settings": {
                "clients": [
                    {
                        "password": "",
                        "level": 0,
                        "email": "$alamat_emel"
                    }
                ],
                "fallbacks": [
                    {
                        "dest": 80
                    }
                ]
            },
            "streamSettings": {
                "network": "tcp",
                "security": "none",
                "tcpSettings": {
                    "acceptProxyProtocol": true
                }
            }
        },
        {
            "port": 1234,
            "listen": "127.0.0.1",
            "protocol": "vless",
            "settings": {
                "clients": [
                    {
                        "id": "",
                        "level": 0,
                        "email": "$alamat_emel"
                    }
                ],
                "decryption": "none"
            },
            "streamSettings": {
                "network": "ws",
                "security": "none",
                "wsSettings": {
                    "acceptProxyProtocol": true,
                    "path": "/websocket"
                }
            }
        },
        {
            "port": 2345,
            "listen": "127.0.0.1",
            "protocol": "vmess",
            "settings": {
                "clients": [
                    {
                        "id": "",
                        "level": 0,
                        "email": "$alamat_emel"
                    }
                ]
            },
            "streamSettings": {
                "network": "tcp",
                "security": "none",
                "tcpSettings": {
                    "acceptProxyProtocol": true,
                    "header": {
                        "type": "http",
                        "request": {
                            "path": [
                                "/vmesstcp"
                            ]
                        }
                    }
                }
            }
        },
        {
            "port": 3456,
            "listen": "127.0.0.1",
            "protocol": "vmess",
            "settings": {
                "clients": [
                    {
                        "id": "",
                        "level": 0,
                        "email": "$alamat_emel"
                    }
                ]
            },
            "streamSettings": {
                "network": "ws",
                "security": "none",
                "wsSettings": {
                    "acceptProxyProtocol": true,
                    "path": "/vmessws"
                }
            }
        }
    ],
    "outbounds": [
        {
            "protocol": "freedom"
        }
    ]
}
xray-xtls
systemctl enable xray@xray-xtls; systemctl start xray@xray-xtls

echo
echo -e "\033[1;32mCongratulation, We are done with the xray installation.\033[0m"
echo
echo 'Use my referral link https://m.do.co/c/a28a40414d6a'
echo 'to gets $100 credit into your DigitalOcean account.'
echo 'Created by Doctype, Powered by Cybertize'
echo 'Copyright 2021, Allright reserved.'
echo; sleep 5
